package com.asdf.db.operations.user;

import java.util.List;

import com.asdf.db.operations.IDBOperation;
import com.asdf.db.results.ResultStatus;
import com.asdf.db.results.user.UserResult;
import com.asdf.external.DBUserDetails;

/** Operation for inserting user.
 * @author so4i2014
 *
 */
public class InsertUser implements IDBOperation< UserResult>
{

	private List< DBUserDetails> userDataSource = null;
	private DBUserDetails userToInsert = null;
	
	/** Create a new operation.
	 * @param source - a list we are going insert to.
	 */
	public InsertUser( List< DBUserDetails> source)
	{
		userDataSource = source;
	}
	
	/** Set the user we are going insert.
	 * @param user
	 */
	public void setUser( DBUserDetails user)
	{
		this.userToInsert = user;
	}
	
	/* (non-Javadoc)
	 * @see com.asdf.db.operations.IDBOperation#perform()
	 */
	@Override
	public UserResult perform( )
	{
		UserResult result = new UserResult( );

		for (DBUserDetails u : userDataSource)
			if (u.getUsername().equalsIgnoreCase( userToInsert.getUsername())) {
				result.setStatus( ResultStatus.AlreadyExist);
				return result;
			}

		userDataSource.add( userToInsert);
		result.setStatus( ResultStatus.OK);
		return result;
	}

}
